package com.ly.user_system.utils;

import cn.hutool.core.util.ObjectUtil;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class ListUtil {
    public static <T, ID> List<T> listToTree(List<T> list, Function<T, ID> tidFunction, Function<T, ID> parentFunction, BiConsumer<T, List<T>> childConsumer) {
        for (T t : list) {
            ID id = tidFunction.apply(t);
            List<T> childList = list.stream().filter(item -> {
                ID parentId = parentFunction.apply(item);
                return ObjectUtil.isNotEmpty(parentId) && parentId.equals(id);
            }).toList();
            childConsumer.accept(t, childList);
        }
        return list.stream().filter(item -> ObjectUtil.isEmpty(tidFunction.apply(item))).toList();
    }

}
